const Koa = require('koa')
const koaBody = require('koa-body')
const routing = require('./routers/index')
const error = require('koa-json-error')
const parameter = require('koa-parameter')
const koaStatic = require('koa-static')
const path = require('path')
const mongoose = require('mongoose')
const app = new Koa();
const {connectionStr}=require('./config')
mongoose.connect(connectionStr,{
    useUnifiedTopology:true,
    useNewUrlParser:true,
},()=>{
    console.log('this.db run in success')
})
mongoose.connection.on('error',console.error);


app.use(koaStatic(path.join(__dirname, 'public')))
app.use(error({
  postFormat: (e, { stack, ...rest}) => process.env.NODE_ENV === 'production' ? rest: {stack, ...rest}
}))
app.use(koaBody({
  multipart: true,
  formidable: {
    uploadDir: path.join(__dirname,'/public/upload'),
    keepExtensions: true
  }
}))
app.use(parameter(app))
routing(app)


app.listen(3000, () => {
  console.log('程序已启动')
})